package com.springboot.shanhexin.controller;

import com.springboot.shanhexin.entity.Page;
import com.springboot.shanhexin.entity.restaurant;
import com.springboot.shanhexin.service.restaurantService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class restaurantCtrl {

    @Autowired
    private restaurantService restaurantService;

    //分页显示全部
    @RequestMapping("/restaurant")
    public String restaurant(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = restaurantService.getCount();
        List<restaurant> restaurantList=new ArrayList<restaurant>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            restaurantList=restaurantService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            restaurantList=restaurantService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_restaurant");
        model.addAttribute("show_left", "left_restaurant");
        model.addAttribute("left", "left_y_restaurant");
        model.addAttribute("mypage", "restaurant");
        model.addAttribute("restaurant", restaurantList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_restaurant";
    }

    //分页显示全部搜索
    @RequestMapping("/searchrestaurant")
    public String searchrestaurant(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="b.name";
        if(types == 1) {
            keyword_new = "b.name";
        }else if (types == 2) {
            keyword_new = "a.name";
        }else if (types == 3) {
            keyword_new = "a.phone";
        }
        totalCount = restaurantService.getsearchCount(keyword_new,keyword);
        List<restaurant> restaurantList=new ArrayList<restaurant>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            restaurantList=restaurantService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            restaurantList=restaurantService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(restaurantList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_restaurant");
            model.addAttribute("show_left", "left_restaurant");
            model.addAttribute("left", "left_y_restaurant");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("restaurant", restaurantList);
            return "houtai_restaurant";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/restaurant?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/addrestaurant")
    @ResponseBody
    public String addrestaurant(restaurant restaurant) {
        restaurantService.add(restaurant);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/updaterestaurant")
    public String updaterestaurant(restaurant restaurant) {
        restaurantService.update(restaurant);
        return "redirect:/restaurant";
    }

    //删除
    @RequestMapping("/deleterestaurant")
    public String deleterestaurant(int userid) {
        restaurantService.delete(userid);
        return "redirect:/restaurant";
    }

    //跳转到更改页面
    @RequestMapping("/to_updaterestaurant")
    public String to_updaterestaurant(Model model, int userid) {
        List<restaurant> restaurantList=new ArrayList<restaurant>();
        restaurantList = restaurantService.getUserone(userid);
        model.addAttribute("top", "top_restaurant");
        model.addAttribute("show_left", "left_updaterestaurant");
        model.addAttribute("left", "left_y_restaurant");
        model.addAttribute("mypage", "restaurant");
        model.addAttribute("restaurant", restaurantList);
        return "houtai_updaterestaurant";
    }

    //跳转到添加页面
    @RequestMapping("/to_addrestaurant")
    public String to_addrestaurant(Model model) {
        model.addAttribute("top", "top_restaurant");
        model.addAttribute("show_left", "left_addrestaurant");
        model.addAttribute("left", "left_y_restaurant");
        model.addAttribute("mypage", "restaurant");
        return "houtai_addrestaurant";
    }

}
